package com.mediacloud.app.newsmodule.fragment.baoliao.frag

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.model.utils.TimeUtils
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.fragment.baoliao.api.MemberApi
import com.mediacloud.app.user.utils.RxUtils
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.clock_in_rank_activity.*
import okhttp3.MediaType
import okhttp3.RequestBody
import org.jetbrains.anko.textColor
import org.json.JSONException
import org.json.JSONObject

/**
 * @author Heaven Created on 2022/3/14
 */
class ClockInRankActivity : BaseBackActivity() {

    lateinit var contentAdapter: ContentAdapter
    var missionId: Long = 0
    var missionTitle: String = ""

    override fun getLayoutResID(): Int {
        return R.layout.clock_in_rank_activity
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        missionId = intent.getLongExtra("missionId", 0)
        missionTitle = intent.getStringExtra("missionTitle") ?: ""

        clock_in_title.text = missionTitle

        back.setOnClickListener { finish() }
        content.layoutManager = LinearLayoutManager(this)
        contentAdapter = ContentAdapter()
        content.adapter = contentAdapter

        getData()
    }

    private fun getData() {
        val obj = JSONObject()
        try {
            obj.put("missionId", missionId)
            val body = RequestBody.create(MediaType.parse("application/json"), obj.toString())
            MemberApi.getMemberApi()
                .rankList(body)
                .compose(RxUtils.schedulersTransformer())
                .subscribe(object : Observer<JSONObject> {
                    override fun onSubscribe(d: Disposable) {
                        mCompositeDisposable.add(d)
                    }

                    override fun onNext(jsonObject: JSONObject) {
                        Log.e(TAG, jsonObject.toString())
                        val data = com.alibaba.fastjson.JSONObject.parseObject(
                            jsonObject.toString(),
                            RankData::class.java
                        )
                        if (data.isSuccess && data.data.size > 0) {
                            contentAdapter.setNewData(data.data)
                        }
                    }

                    override fun onError(e: Throwable) {
                        e.printStackTrace()
                        Log.e(TAG, e.message + "")
                    }

                    override fun onComplete() {}
                })
        } catch (e: JSONException) {
            e.printStackTrace()
        }

    }

    override fun getFitSystemWindow(): Boolean {
        return true
    }

    override fun getStatusBarColor(): Int {
        return Color.WHITE
    }

    class ContentAdapter :
        BaseQuickAdapter<RankData.DataBean, BaseViewHolder>(R.layout.clock_in_rank_item) {

        @SuppressLint("SetTextI18n")
        override fun convert(helper: BaseViewHolder, item: RankData.DataBean?) {
            val rank: TextView = helper.getView(R.id.rank)
            val username: TextView = helper.getView(R.id.username)
            val time: TextView = helper.getView(R.id.time)
            val total: TextView = helper.getView(R.id.total)

            item?.let {
                when (helper.adapterPosition) {
                    0 -> rank.textColor = Color.parseColor("#D9001B")
                    1 -> rank.textColor = Color.parseColor("#9AB7D8")
                    2 -> rank.textColor = Color.parseColor("#FF8D3B")
                    else -> rank.textColor = Color.parseColor("#333333")
                }
                rank.text = "${helper.adapterPosition + 1}"
                username.text = it.nickName
                time.text = TimeUtils.toFormat(it.createTime, "HH:mm:ss")
                total.text = "${it.accumulate}次"

            }
        }
    }
}